<?php
class sort {
    private $str;
    public function __construct($str) {
        $this->str=strtolower($str);
    }
    private function explodes() {
        if(empty($this->str)) return array();
        $arr=explode(" ",$this->str);
        return is_array($arr)?$arr:array($arr);
    }
    public function sort() {
        $explode=$this->explodes();
        sort($explode);
        return $explode;
    }
}


class file {
    private $sort=null;
    private $filepath;
    public function __construct($arrobj,$path) {
        $this->sort=$arrobj;
        $this->filepath=$path;
    }
    private function getresource($filename,$mode) {
        return fopen($this->filepath.$filename,$mode);
    }
    private function closeresource($resource) {
        fclose($resource);
    }
    public function savefile($filename) {
        $arr=$this->sort->sort();
        $fopen=$this->getresource($filename,"a+");
        if(!$fopen){
            echo "文件打开失败！";
            exit;
        }
        var_dump($arr);
        foreach($arr as $key=>$value) {
            fwrite($fopen,$value."\n");
        }
        $this->closeresource($fopen);
    }
    public function readfile($filename) {
        $this->savefile($filename);
        $fopen=$this->getresource($filename,"r");
        if(!$fopen){
            echo "文件打开失败！";exit;
        }
        $arr=array();
        while(!feof($fopen)) {
            $get=fgets($fopen);
            if(!empty($get))
                $arr[]=str_replace("\n","",$get);
        }
        $this->closeresource($fopen);
        return $arr;
    }
}

//$str='Apple Orange Banana Strawberry';
//$sortob=new sort($str);
//var_dump($sortob->sort());

# 请写一段程序，在服务器创建一个文件fruit.dat,将试题3中得到的数组写入到改文件中，
# 然后写一段程序从文件中读取并还原数组@author zhuwenqiong
$file=new file(new sort('Apple Orange Banana Strawberry'),"E:\\");
$arr=$file->readfile("fruit.dat");
var_dump($arr);